package com.xx.rpc.core.remoting.net;

import com.xx.rpc.core.remoting.net.params.BaseCallback;
import com.xx.rpc.core.remoting.provider.XxRpcProviderFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class Server {

    protected static final Logger logger = LoggerFactory.getLogger(Server.class);


    private BaseCallback startedCallback;
    private BaseCallback stopedCallback;

    public void setStartedCallback(BaseCallback startedCallback) {
        this.startedCallback = startedCallback;
    }

    public void setStopedCallback(BaseCallback stopedCallback) {
        this.stopedCallback = stopedCallback;
    }


    /**
     * 开始提供者服务
     *
     * @param xxlRpcProviderFactory 提供者工厂
     * @throws Exception 错误信息
     */
    public abstract void start(final XxRpcProviderFactory xxlRpcProviderFactory) throws Exception;

    /**
     * 查询执行中的服务回调
     */
    public void onStarted() {
        if (startedCallback != null) {
            try {
                startedCallback.run();
            } catch (Exception e) {
                logger.error(">>>>>>>>>>> xx-rpc, 启动中的服务回调报错", e);
            }
        }
    }

    /**
     * 暂停服务
     *
     * @throws Exception 错误信息
     */
    public abstract void stop() throws Exception;

    /**
     * 查询暂停中的服务回调
     */
    public void onStoped() {
        if (stopedCallback != null) {
            try {
                stopedCallback.run();
            } catch (Exception e) {
                logger.error(">>>>>>>>>>> xx-rpc, 暂停中的服务回调报错.", e);
            }
        }
    }

}
